﻿using System;
using System.Text;
using System.Drawing;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

public static partial class NativeAOT
{
    [UnmanagedCallersOnly(EntryPoint = "hamming")]
    public static unsafe void hamming(double t, double h, int n, IntPtr y_ptr, double eps, int k, IntPtr z_ptr, IntPtr f_x_ya_n_da_ptr)
    {
        double* y = (double*)y_ptr.ToPointer();
        double* z = (double*)z_ptr.ToPointer();
        f_x_ya_n_da = Marshal.GetDelegateForFunctionPointer<delegatefunc_x_ya_n_da>(f_x_ya_n_da_ptr);

        hamming(t, h, n, y, eps, k, z);
    }

    /// <summary>
    /// 全区间积分Hamming方法
    /// f计算微分方程组中各方程右端函数值的函数名。
    /// </summary>
    /// <param name="t">积分起始点。</param>
    /// <param name="h">积分步长。</param>
    /// <param name="n">一阶微分方程组中方程个数，也是未知函数个数。</param>
    /// <param name="y">y[n]存放n个未知函数在起始点t处的函数值。</param>
    /// <param name="eps">eps变步长Runge_Kutta法的控制精度要求。</param>
    /// <param name="k">积分步数（包括起始点这一步）。</param>
    /// <param name="z">z[n][k]返回k个积分点（包括起始点）上的未知函数值。</param>
    public static unsafe void hamming(double t, double h, int n, double* y, double eps, int k, double* z)
    {
        int i, j, m;
        double a, q;
        double* b = stackalloc double[4 * n];
        double* d = stackalloc double[n];
        double* u = stackalloc double[n];
        double* v = stackalloc double[n];
        double* w = stackalloc double[n];
        double* g = stackalloc double[n];
        a = t;
        for (i = 0; i <= n - 1; i++) z[i * k] = y[i];
        f_x_ya_n_da(t, y, n, d);
        for (i = 0; i <= n - 1; i++) b[i] = d[i];
        for (i = 1; i <= 3; i++)
            if (i <= k - 1)
            {
                t = a + i * h;
                runge_kutta(t, h, n, y, eps);
                for (m = 0; m <= n - 1; m++) z[m * k + i] = y[m];
                f_x_ya_n_da(t, y, n, d);
                for (m = 0; m <= n - 1; m++) b[i * n + m] = d[m];
            }
        for (i = 0; i <= n - 1; i++) u[i] = 0.0;
        for (i = 4; i <= k - 1; i++)
        {
            for (j = 0; j <= n - 1; j++)
            {
                q = 2.0 * b[3 * n + j] - b[n + n + j] + 2.0 * b[n + j];
                y[j] = z[j * k + i - 4] + 4.0 * h * q / 3.0;
            }
            for (j = 0; j <= n - 1; j++) y[j] = y[j] + 112.0 * u[j] / 121.0;
            t = a + i * h;
            f_x_ya_n_da(t, y, n, d);
            for (j = 0; j <= n - 1; j++)
            {
                q = 9.0 * z[j * k + i - 1] - z[j * k + i - 3];
                q = (q + 3.0 * h * (d[j] + 2.0 * b[3 * n + j] - b[n + n + j])) / 8.0;
                u[j] = q - y[j];
                z[j * k + i] = q - 9.0 * u[j] / 121.0;
                y[j] = z[j * k + i];
                b[n + j] = b[n + n + j];
                b[n + n + j] = b[n + n + n + j];
            }
            f_x_ya_n_da(t, y, n, d);
            for (m = 0; m <= n - 1; m++) b[3 * n + m] = d[m];
        }
        return;
    }

    /*
    // 全区间积分Hamming方法例
      int main()
      { 
          int i,j;
          void  hamgf(double,double [],int,double []);
          double y[3],z[3][11];
          double t,h,eps;
          t=0.0; h=0.1; eps=0.0000001;
          y[0]=1.0; y[1]=1.0; y[2]=1.0;
          hamming(t,h,3,y,eps,11,&z[0][0],hamgf);
          for (i=0; i<=10; i++)
          { 
              t=i*h;
              cout <<"t = " <<t;
              for (j=0; j<=2; j++)
                  cout <<"  y(" <<j <<") = " <<setw(10) <<z[j][i];
              cout <<endl;
          }
          return 0;
      }
    // 计算微分方程组中各方程右端函数值
      void hamgf(double t, double y[], int n, double d[])
      { 
          t=t; n=n;
          d[0]=y[1]; d[1]=-y[0]; d[2]=y[2];
          return;
      }
    */
}

